#include "App.h"
#include "GateServerRoot.h"
static int g_bTerminated = false;
#ifdef WIN32
#include <Windows.h>
BOOL WINAPI console_handler(DWORD cevent)
{
    switch(cevent)
    {
    case CTRL_C_EVENT:
    case CTRL_BREAK_EVENT:
    case CTRL_CLOSE_EVENT:
    case CTRL_LOGOFF_EVENT:
    case CTRL_SHUTDOWN_EVENT:
        {
            //your code here
            g_bTerminated = true;
            break;
        }
    default:
        {
        }
    }
    return TRUE;
};
#else
void term_handler(int sig)
{
    g_bTerminated = true;

}
#endif

CApp::CApp(void)
{
}


CApp::~CApp(void)
{
    GateServerRoot::DestroyInstance();
}

void CApp::Run()
{
#ifdef WIN32
    if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)console_handler,TRUE)==FALSE)
    {
        return;
    }
#else
    signal(SIGTERM,term_handler) ;
    signal(SIGINT,term_handler) ;
    signal(SIGUSR2,term_handler) ;
    signal(SIGALRM,SIG_IGN);
#endif
    if (GateServerRoot::Instance()->Init())
    {
        while(!g_bTerminated)
        {
            if (!GateServerRoot::Instance()->RunOnce())
                Sleep(1);
        }
    }
}


